﻿Imports System.Data.OleDb
Public Class ConversionTables
    Inherits System.Web.UI.Page

    Dim multiplier As Double
    Dim result As Double
    Dim categoryName As String
    Dim dataSource As SqlDataSource
    Dim listOfUnits As List(Of String)
    Dim pstBckCntrlNme As String = ""

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack AndAlso Not String.IsNullOrEmpty(tb_From.Text) Then
            categoryName = ddl_Category.SelectedValue
            ddl_FromUnit.DataSource = getDataSource(categoryName, "FromUnit")
            ddl_ToUnit.DataSource = getDataSource(categoryName, "ToUnit")
            ddl_FromUnit.DataBind()
            ddl_ToUnit.DataBind()
            pstBckCntrlNme = getPostBackControlId()
            If pstBckCntrlNme = tb_From.UniqueID Or pstBckCntrlNme = ddl_FromUnit.UniqueID Then
                convert(tb_From.Text, ddl_FromUnit.SelectedValue.ToString, ddl_ToUnit.SelectedValue)
                tb_ToUnit.Text = result.ToString()
                tb_ToUnit.DataBind()
            Else
                If pstBckCntrlNme = tb_ToUnit.UniqueID Or pstBckCntrlNme = ddl_ToUnit.UniqueID Then
                    convert(tb_ToUnit.Text, ddl_ToUnit.SelectedValue.ToString, ddl_FromUnit.SelectedValue)
                    tb_From.Text = result.ToString()
                    tb_From.DataBind()
                End If

            End If

            'lbl_Result.Text = result.ToString()
            'lbl_Result.DataBind()
        End If
    End Sub

    Function getPostBackControlId()
        'Dim control As Control
        pstBckCntrlNme = Page.Request.Params.Get("__EventTarget")
        Return pstBckCntrlNme
        'If Not String.IsNullOrEmpty(controlName) Then
        '    control = Page.FindControl(controlName)
        '    Return controlName
        'Else
        'Dim controlString As String = String.Empty
        'Dim c As Control

        'End If
    End Function

    Function getDataSource(ByVal category As String, ByVal direction As String)
        listOfUnits = New List(Of String)
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("StaarshipDB").ToString())
        Dim sql As String = "SELECT " & direction & " FROM " & category
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        conn.Open()
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.HasRows Then
            While (reader.Read())
                listOfUnits.Add(reader(direction))
            End While
        End If

        Return listOfUnits
    End Function

    'Dim ListOfAreaUnits As List(Of String) = New List(Of String)
    'Sub addUnitNameToCategory(ByVal unitName As String, ByVal categoryName As String)

    'End Sub

    Function convert(ByVal fromValue As Double, ByVal fromUnit As String, ByVal toUnit As String)
        getMultiplier()
        result = fromValue * multiplier
        Return result
    End Function
    Function getMultiplier()
        Dim getMultiplierSQL As String
        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("StaarshipDB").ToString())
        'Dim getMultiplierSQL As String = "SELECT Multiplier FROM ConversionTables WHERE FromUnit=" & fromUnit.ToString() & "AND ToUnit=" & toUnit.ToString()
        If pstBckCntrlNme = tb_From.UniqueID Then
            getMultiplierSQL = "SELECT Multiplier FROM " & categoryName & " WHERE FromUnit = '" & ddl_FromUnit.SelectedValue & "' AND ToUnit = '" & ddl_ToUnit.SelectedValue & "'"
        Else
            getMultiplierSQL = "SELECT Multiplier FROM " & categoryName & " WHERE FromUnit = '" & ddl_ToUnit.SelectedValue & "' AND ToUnit = '" & ddl_FromUnit.SelectedValue & "'"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(getMultiplierSQL, conn)
        'cmd.Parameters.AddWithValue("@FromUnit", ddl_From.SelectedValue.ToString)
        'cmd.Parameters.AddWithValue("@ToUnit", ddl_To.SelectedValue.ToString)
        conn.Open()
        Dim reader As OleDbDataReader = cmd.ExecuteReader()
        If reader.Read() Then
            multiplier = CType(reader("Multiplier"), Double)
        End If
        If multiplier = -1 Then
            multiplier = 1
        End If
        Return multiplier
    End Function
    'End Sub
End Class